package com.yycx.module.bus.provider.handler.process;

import com.yycx.bpm.provider.common.handler.ProcessStartHandler;
import com.yycx.bpm.provider.model.ProcessInstanceRequest;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.admin.provider.mapper.BaseRoleUserMapper;
import com.yycx.module.admin.provider.mapper.BaseUserCompanyMapper;
import com.yycx.module.bus.client.entity.BusOrder;
import com.yycx.module.bus.provider.service.BusOrderService;
import com.yycx.msg.provider.websocket.WebSocketServerEndpoint;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 启动订单审核流程
 *
 * @author zyf
 */
@Component
public class OrderStartProcessHandler implements ProcessStartHandler {

    @Autowired
    private BusOrderService orderService;
    @Resource
    private WebSocketServerEndpoint socketServerEndpoint;
    @Resource
    private BaseRoleUserMapper roleUserMapper;

    /**
     * 初始化流程参数
     *
     * @param processInstanceRequest
     * @return
     */
    @Override
    public String init(ProcessInstanceRequest processInstanceRequest) {
        Long companyId = OpenHelper.getCompanyId();
        processInstanceRequest.setProcessKey("process_order_" + companyId);
        return processInstanceRequest.getBusinessKey();
    }

    /**
     * 设置全局流程变量
     *
     * @param processInstanceRequest
     * @param definition
     * @return
     */
    @Override
    public Map<String, Object> initVariables(ProcessInstanceRequest processInstanceRequest, ProcessDefinition definition) {
        Map<String, Object> map = new HashMap<>();
        BusOrder order = orderService.getById(processInstanceRequest.getBusinessKey());
        if(!order.getOrderState().equals(0)){
            ApiAssert.failure("请勿重复提交");
        }
        Integer customBondType = order.getCustomBondType();
        //是否出纳认款
        if (customBondType.equals(5) || customBondType.equals(0)|| customBondType.equals(7)) {
            map.put("renkuan", 0);
        } else {
            map.put("renkuan", 1);
        }
        Integer delivery = FlymeUtils.getInteger(order.getDelivery(), 1);
        map.put("delivery", delivery);
        map.put("orderNo", order.getOrderNo());
        map.put("companyId", order.getCompanyId());
        map.put("deptId", order.getDeptId());
        return map;
    }

    /**
     * 启动完成业务处理
     *
     * @param processInstanceRequest
     * @param definition
     * @param processInstance
     * @param tasks
     * @param variables
     */
    @Override
    public Boolean afterComplete(ProcessInstanceRequest processInstanceRequest, ProcessDefinition definition, ProcessInstance processInstance, List<Task> tasks, Map<String, Object> variables) {

        String businessKey = processInstanceRequest.getBusinessKey();
        Object orderNo=variables.get("orderNo");
        Object companyId=variables.get("companyId");
        //更新订单为已提交并管理流程实例ID
        orderService.updateOrderState(Long.parseLong(businessKey), processInstance.getProcessInstanceId(), CommonConstants.ENABLED);
        List<Long> userIds = roleUserMapper.selectUserIdByRoleCode("ROLE_NQBZ",Long.parseLong(companyId.toString()));
        socketServerEndpoint.sendMessage(2L, "订单审核","您有新的订单待审核,单号:"+orderNo, userIds, 2);
        return  true;
    }
}
